/**
 * Created by Dalyn on 2017/2/28.
 */
import React from "react";
import { hashHistory,Link } from 'react-router';
import Confirm from '../../common/Confirm'
import Relay from "react-relay";
import ListOrderMutation from "./mutation/ListOrderMutation"
import Order_Item from './Order_Item'
import './css/IndentManage.css'
import Util from "../../util/Util"
import Pagination from "./Pagination"
import Reactkey from "../../util/ReactKey"
import "./css/ListOrder_SaleAndRe.css"
/*信息列表*/
export default class ListOrder extends React.Component{
    constructor(props){
        super(props);
        this.state={
            orders:[],
            totalresult:0,
            placeholder:'请输入企业名称进行搜索'
        };
        this.odtail='1';
        this.ods='';
        this.enid = sessionStorage.getItem('enterpriseId');
        this.serviceType = sessionStorage.getItem('serviceType');
        this.goodtypeobj={'1':'成衣','2':'辅料','3':'面料','4':'印染','5':'纱线'}
    }


    componentWillMount(){
        this.queryorderlist(10,1);
    }
    componentDidMount(){

    }


    queryorderlist(pageSize,pageNum,enName){
        let orderType = '';
        if(this.serviceType ==1){
            orderType = '1'
        }else if(this.serviceType ==2){
            orderType = '2'
        }else if(this.serviceType ==3){
            orderType = '3'
        }else if(this.serviceType ==4){
            orderType = '4'
        }else if(this.serviceType ==5){
            orderType = '5'
        }else if(this.serviceType ==6){
        }

        Relay.Store.commitUpdate(
            new ListOrderMutation({
                pageSize: pageSize,
                pageNum: pageNum,
                status:'3,4',
                type:this.odtail,
                enId:this.enid,
                enName:enName||'',
                searchType:1,
                // orderType:orderType
            }),
            {
                onFailure: () => {
                    alert('请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.listOrder.listOrder.errorCode;
                    if(errorCode=='0'){
                        let orders = response.listOrder.listOrder.orders
                        let totalResult = response.listOrder.listOrder.totalResult
                        this.setState({
                            orders:orders,
                            totalresult:totalResult
                        });
                    }
                    // console.log(response )
                }
            }
        )
    }

    sendgoods(status,oid,partb,type){
        if(status!=3){
            return
        }
        hashHistory.push('/sendGoods/'+oid)

    }

    jumpGoodsRecord(oid){
        hashHistory.push('/sendGoodsRecord/'+oid)
    }


    jumpDetil(oty,oid){
        switch (oty){
            case 0:

                return;
            case 1:
                hashHistory.push('/orderdetailgms/'+oid);
                // hashHistory.push({pathname:'/orderdetailgms/',state:{'oid':oid}})
                return;
            case 2:
                hashHistory.push('/orderdetailraw/'+oid);
                // hashHistory.push({pathname:'/orderdetailraw/',state:{'oid':oid}})
                return;
            case 3:
                hashHistory.push('/orderdetailcloth/'+oid);
                // hashHistory.push({pathname:'/orderdetailcloth/',state:{'oid':oid}})
                return;
            case 4:
                hashHistory.push('/orderdetailprint/'+oid);
                // hashHistory.push({pathname:'/orderdetailprint/',state:{'oid':oid}})
                return;
            case 5:
                hashHistory.push('/orderdetailyarn/'+oid);
                // hashHistory.push({pathname:'/orderdetailprint/',state:{'oid':oid}})
                return;

        }
    }
    jumptree(oid){
        hashHistory.push({pathname:'/ordertree/',state:{'oid':oid}})
    }



    addDepartmenlist(){
        return this.state.orders.map((context,index)=>{
            return (
                <thead key={index+'-'}>
                    <tr className={index%2==0?"order_list_saleandre_from_trchange":"order_list_saleandre_from_tr"}>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_oid_context" onClick={this.jumptree.bind(this,context.oid)}> {context.oid}</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_partb">{context.partAName}</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_product">{this.goodtypeobj[context.orderType]}</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_count">{Number(context.orderGoodsNum).toFixed(3)}</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_money">{context.account/100}</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_money">{context.payAccount/100}</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_doitcontext" onClick={this.sendgoods.bind(this,context.status,context.oid,context.partB,context.orderType)}>{
                            context.status==3?
                                <div className="order_list_saleandre_from_td_doitcontext_div">{'发货'}</div>
                                :
                                '交易完成'
                        }</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_endcontext" onClick={this.jumpGoodsRecord.bind(this,context.oid)}>查看</td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_endcontext"><Link to = {{pathname:"OrderProgress", query:{oid: context.oid}}}>查看</Link></td>
                        <td className="order_list_saleandre_from_td order_list_saleandre_from_td_endcontext" onClick={this.jumpDetil.bind(this,context.orderType,context.oid)}>查看</td>
                    </tr>
                </thead>
            );
        });
    }

    stroke(){

    }

    status(){
        return this.oArr.map((item,index)=>{
            return (
                <span style={{width:80,height:27,borderRadius:"8px",background:"#ccc",display:"block"}} onClick={this.stroke.bind(this)}>{item}</span>
            )
        })
    }

    createorder(){
        hashHistory.push('/createorder')
    }

    onSure(size,num){
        this.queryorderlist(size,num);
    }

    onfocusInput(){
        this.setState({
            placeholder:''
        })
    }


    bluronInput(){
        this.setState({
            placeholder:'请输入企业名称进行搜索'
        })
    }

    _SearchByEn(){
        let enName = this.refs.enName.value
        this.queryorderlist(10,1,enName);
    }

    render(){
        return (
            <div className="order_box" style={{width:1233,minHeight:800,background:"#ffffff",marginTop:"10px",paddingTop:"30px"}}>
                <div className="order_saleandre_frome_search_box">
                    <input className="order_saleandre_frome_search_input" placeholder={this.state.placeholder}
                           onFocus={this.onfocusInput.bind(this)}
                           onBlur={this.bluronInput.bind(this)} ref={'enName'}/>
                    <div className="order_saleandre_frome_search_div" onClick={this._SearchByEn.bind(this)}>搜索</div>
                </div>

                <div style={{width:1195,height:"800px",background:"#fff",marginTop:"20px",padding:"0 20px 0",position:"relative"}}>
                    <table className="order_list_saleandre_form_table">
                        <thead>
                        <tr className="order_list_saleandre_from_thr">
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_oid">订单编号</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_partb">甲方</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_product">商品</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_count">数量</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_money">订单金额（元）</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_money">已接收金额（元）</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_doit">操作</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_end">发货记录</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_end">订单进度</td>
                            <td className="order_list_saleandre_from_thd order_list_saleandre_from_td_end">订单详情</td>
                        </tr>
                        </thead>
                        {
                            this.addDepartmenlist()
                        }
                    </table>
                    <Pagination eachSize={10} dataSize={this.state.totalresult} onSure={this.onSure.bind(this)} ref={'pagination'}/>
                </div>
            </div>

        )
    }
}
